//
//  PlayCollectionViewCell.m
//  MMLOOK
//
//  Created by dllo on 15/10/9.
//  Copyright © 2015年 武俊娥. All rights reserved.
//

#import "PlayCollectionViewCell.h"
#import "AsynConnect.h"
#import "Play.h"
#import "PlayTableViewCell.h"
#import "MBProgressHUD.h"
#import "GSAFNetWorkTool.h"
#import "PlayViewController.h"

#define playHeight [UIScreen mainScreen].bounds.size.width * 777 / 1242

@interface PlayCollectionViewCell ()<UITableViewDataSource,UITableViewDelegate>

@property (retain, nonatomic) UITableView *tableView;
@property (retain, nonatomic)NSMutableArray *arr;


@end

@implementation PlayCollectionViewCell


-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    
    if (self) {
        
        
        self.arr = [NSMutableArray array];
        
        self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) style:UITableViewStylePlain];
        self.tableView.delegate = self;
        self.tableView.dataSource = self;
        [self.contentView addSubview:self.tableView];
        [_tableView release];
        
        [self networking];
        
        
    }
    
    return self;
}

-(void)networking
{
    // 添加加载动画
    MBProgressHUD *mbProView = [[[MBProgressHUD alloc] initWithView:self] autorelease];
    mbProView.mode = MBProgressHUDModeIndeterminate;// 样式 菊花
    mbProView.labelText = @"(｢･ω･)｢ 加载中";
    [mbProView show:YES];
    [self addSubview:mbProView];
    
    
    [GSAFNetWorkTool getUrl:@"http://baobab.wandoujia.com/api/v1/videos?categoryName=%E6%97%B6%E5%B0%9A&udid=4f15d15af94c4d18bc94b85a3abd8b533fb4621f&vc=51&vn=1.6.1&deviceModel=MI%202S&first_channel=eyepetizer_xiaomi_market&last_channel=eyepetizer_xiaomi_market" body:nil result:GSJSON headerFile:nil success:^(id result) {
        
        [mbProView hide:YES];
        
        NSMutableArray *arr = [result objectForKey:@"videoList"];
        
        for (NSMutableDictionary *dic in arr) {
            
            Play *play = [[Play alloc]initWithDic:dic];
            
            [self.arr addObject:play];
        
        }
        
        [self.tableView reloadData];
     
        
    } failure:^(NSError *error) {
        
    }];
    
    
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    PlayViewController *play = [[PlayViewController alloc]init];
    [self.root.navigationController pushViewController:play animated:YES];
    
    play.arr = [NSMutableArray arrayWithArray:self.arr];
    play.count = indexPath.item;
    
    [play release];
    
    
}



-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    return playHeight;
}


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.arr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
   static NSString *str = @"a";
    
    PlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        
        cell = [[[PlayTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str]autorelease];
    }
    
    cell.play = [self.arr objectAtIndex:indexPath.item];
    
    return cell;
    
}


- (void)awakeFromNib {
    // Initialization code
    
    
    
}

- (void)dealloc {
    [_tableView release];
    [super dealloc];
}
@end
